package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.mapper.*;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDishes;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :SetMealServiceImpl
 * creationTime :2022-11-24 20:16
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;

    @Transactional
    @Override
    public Boolean insertSetMeal(SetmealVo setmealVo) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVo,setmeal);
        setmeal.setId(IdUtil.getSnowflakeNextIdStr());
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(ThreadLocalUtil.getid());
        setmeal.setUpdateUser(ThreadLocalUtil.getid());
        Integer insert=setmealMapper.insertSetMeal(setmeal);
        List<SetmealDishes> setmealDishes = setmealVo.getSetmealDishes();
        if(setmealDishes.size()==0){
            return insert>0;
        }
        beforeSetmealDishesInset(setmeal, setmealDishes);
        Integer insertListSetMealDishea=setmealDishesMapper.insertListSetMealDishea(setmealDishes);
        return insert>0&&insertListSetMealDishea>0;
    }

    private static void beforeSetmealDishesInset(Setmeal setmeal, List<SetmealDishes> setmealDishes) {
        for (SetmealDishes setmealDish : setmealDishes) {
            setmealDish.setId(IdUtil.getSnowflakeNextIdStr());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(ThreadLocalUtil.getid());
            setmealDish.setUpdateUser(ThreadLocalUtil.getid());
        }
    }

    @Transactional
    @Override
    public Boolean updateSetmeal(SetmealVo setmealVo) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVo,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(ThreadLocalUtil.getid());
        Integer update=setmealMapper.updateSetmeal(setmeal);
        List<SetmealDishes> setmealDishes = setmealVo.getSetmealDishes();
        if(setmealDishes.size()==0){
            return update>0;
        }
        setmealDishesMapper.deleteBySetmealId(setmealVo.getId());
        beforeSetmealDishesInset(setmeal,setmealDishes);
        Integer insertListSetMealDishea = setmealDishesMapper.insertListSetMealDishea(setmealDishes);

        return update>0&&insertListSetMealDishea>0;
    }

    @Override
    public PagingQueryVo<Setmeal> selectPage(String name, Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<Setmeal> setmeals=setmealMapper.selectPage(name,start,pageSize);
        if (setmeals.size()==0){
            return null;
        }
        Set<String> categories = setmeals.stream().map(Setmeal::getCategoryId).collect(Collectors.toSet());
        List<Category> categoriesList = categoryMapper.selectByCategoryIds(categories);
        Map<String, List<Category>> categoriesMap = categoriesList.stream().collect(Collectors.groupingBy(Category::getId));

        for (Setmeal setmeal : setmeals) {
            setmeal.setCategoryName(categoriesMap.get(setmeal.getCategoryId()).get(0).getName());
        }
        PagingQueryVo<Setmeal> pagingQueryVo = new PagingQueryVo<>();
        pagingQueryVo.setRecords(setmeals);
        pagingQueryVo.setSize(pageSize);
        pagingQueryVo.setCurrent(page);
        pagingQueryVo.setTotal(setmealMapper.countTotal(name));
        return pagingQueryVo;
    }


    @Override
    public SetmealVo selectById(String id) {
        Setmeal setmeal=setmealMapper.selectById(id);
        List<SetmealDishes> setmealDishes=setmealDishesMapper.selectBySetmealId(setmeal.getId());
        SetmealVo setmealVo = new SetmealVo();
        BeanUtils.copyProperties(setmeal,setmealVo);
        setmealVo.setSetmealDishes(setmealDishes);
        setmealVo.setCategoryName(categoryMapper.selectByCategoryId(setmeal.getCategoryId()).getName());
        return setmealVo;
    }

    @Transactional
    @Override
    public Integer deleteSetmeal(List<String> ids) {

        List<Integer> status=setmealMapper.selectStatusByIds(ids);
        for (Integer info : status) {
            if (info==1){
                log.warn("你要删除的ID中有正在起售的商品，不能删除！");
                return -1;
            }
        }
        Integer deletesetmealdishs=setmealDishesMapper.deleteBySetmealIds(ids);
        Integer deletesetmeal=setmealMapper.deleteByids(ids);
        if(deletesetmeal>0&&deletesetmealdishs>0){
            return 1;
        }
        return 0;
    }

    @Override
    public Boolean changeStatus(Integer status, List<String> ids) {
        LocalDateTime updateTime=LocalDateTime.now();
        String updateUser=ThreadLocalUtil.getid();
        Integer change=setmealMapper.updateStatus(status,ids,updateTime,updateUser);
        return change>0;
    }

    @Override
    public List<UserSetmealVo> selectSetmeals(String categoryId, Integer status) {
        List<Setmeal> setmeals=setmealMapper.selectSetmeals(categoryId, status);
        List<UserSetmealVo> setmealVos = BeanUtil.copyToList(setmeals, UserSetmealVo.class);
//        List<UserSetmealVo> setmealVos=new ArrayList<>();
//        for (Setmeal setmeal : setmeals) {
//            UserSetmealVo usersetmealVo = new UserSetmealVo();
//            BeanUtils.copyProperties(setmeal,usersetmealVo);
//            setmealVos.add(usersetmealVo);
//        }
        return setmealVos;
    }

    @Override
    public UserSetmealVo selectDishesById(String id) {
        Setmeal setmeal=setmealMapper.selectById(id);
        if (setmeal==null){
            return null;
        }
        List<SetmealDishes> setmealDishes = setmealDishesMapper.selectBySetmealId(id);
        if (setmealDishes==null){
            return null;
        }
        Set<String> dishIds = setmealDishes.stream().map(SetmealDishes::getDishId).collect(Collectors.toSet());
        Map<String, List<SetmealDishes>> setmealDishesMap = setmealDishes.stream().collect(Collectors.groupingBy(SetmealDishes::getDishId));

        List<UserDishVo> userDishVos=dishService.selectByDishIds(dishIds);
        for (UserDishVo userDishVo : userDishVos) {
            userDishVo.setCopies(setmealDishesMap.get(userDishVo.getId()).get(0).getCopies());
        }
        UserSetmealVo userSetmealVo = new UserSetmealVo();
        BeanUtils.copyProperties(setmeal,userSetmealVo);
        userSetmealVo.setSetmealDishes(userDishVos);
//        List<UserDishVo> userDishVos = new ArrayList<>();
//        for (SetmealDishes setmealDish : setmealDishes) {
//            UserDishVo userDishVo = dishService.selectByDishId(setmealDish.getDishId());
//            userDishVo.setCopies(setmealDish.getCopies());
//            userDishVos.add(userDishVo);
//        }
//        UserSetmealVo userSetmealVo = new UserSetmealVo();
//        BeanUtils.copyProperties(setmeal,userSetmealVo);
//        userSetmealVo.setSetmealDishes(userDishVos);
        return userSetmealVo;
    }




}
